<?php

class Workon extends CI_Controller {
 
    public function __construct()
    {
        parent::__construct();
        $this->load->model('workon_model');
    }
    
    public  function index(){
       if($this->session->userdata('is_logged_in')){
            $this->list_workon();
        }else{
            $this->load->view('admin/login');   
        }
 
    }

    public function  list_workon(){
        $return_value = $this->workon_model->get_workon();
        $data['workon'] =$return_value['result'];
        $data['message'] =$return_value['message'];
        $data['state'] =$return_value['state'];
        $data['main_content'] = 'admin/workon/list';
        $this->load->view('includes/template', $data); 
    }
    
    public function add()
    {
        $this->load->model('workon_model');
           date_default_timezone_set('America/Los_Angeles');
        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
            //form validation
            $this->form_validation->set_rules('empp_id', 'Employee ID', 'required');
            $this->form_validation->set_rules('proj_id', 'Project ID', 'required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
                $data_to_store = array(
                    'empp_id' => $this->input->post('empp_id'),
                    'proj_id' => $this->input->post('proj_id'),
                );
               
                $data = $data_to_store;                
                
               $return_value = $this->workon_model->store_workon($data_to_store);

              $data['workon'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];
               
               if($return_value['state']== 1){
                    $this->session->set_flashdata('flash_message', 'added');
                    redirect('index.php/workon');
               } else {
                    $this->session->set_flashdata('flash_message', 'not_added');
                    redirect('index.php/workon/add');
               }
               

            }
        }
        
         $return_value = $this->workon_model->get_workon();
         $data['workon'] =$return_value['result'];
         $data['message'] =$return_value['message'];
         $data['state'] =$return_value['state'];
         $data['main_content'] = 'admin/workon/add';
         $this->load->view('includes/template', $data); 

        
    }

    public function update()
    {
           $this->load->model('workon_model');
           date_default_timezone_set('America/Los_Angeles');
        //product id 
        $id = $this->uri->segment(3);
        //if save button was clicked, get the data sent via post
        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
            //form validation
           //form validation
            $this->form_validation->set_rules('empp_id', 'Employee ID', 'required');
            $this->form_validation->set_rules('proj_id', 'Project ID', 'required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            if ($this->form_validation->run())
            {
    
                $data_to_store = array(
                    'empp_id' => $this->input->post('empp_id'),
                    'proj_id' => $this->input->post('proj_id'),
                );
               
                //if the insert has returned true then we show the flash message
               
                                             
                $return_value = $this->workon_model->update_workon($data_to_store);
                
               $data['workon'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];
               if($return_value['state']== 1){
                     $this->session->set_flashdata('flash_message', 'updated');
                    redirect('index.php/workon');
               } else {
                     $this->session->set_flashdata('flash_message', 'not_updated');
                    redirect('index.php/workon/update/'.$data_to_store['empp_id'].'');
               }
               
               
//           $data['info'] =$sql;
//           $data['main_content'] = 'test';
//           $this->load->view('includes/template', $data); 

            }//validation run

        }

        //department data
         $return_value = $this->workon_model->get_workon_by_id($id);
         $array_department =$return_value['result'];
         $data['workon'] = $array_department[0];
         $data['message'] =$return_value['message'];
         $data['state'] =$return_value['state'];
    
      
        //load the view
        $data['main_content'] = 'admin/workon/edit';
        $this->load->view('includes/template', $data);            

    }//update
     /**
    * Delete product by his id
    * @return void
    */
    public function delete()
    {
          $id = $this->uri->segment(3);
          $return_value = $this->workon_model->delete_workon($id);
               $data['workon'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];

               if($return_value['state']== 1){
                     $this->session->set_flashdata('flash_message', 'deleted');
                    redirect('index.php/workon');
               } else {
                     $this->session->set_flashdata('flash_message', 'not_deleted');
                    redirect('index.php/workon');
               }
    }//edit
    
    
}

